package task.manager.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.PriorityQueue;

import task.manager.R;
import task.manager.R.drawable;
import task.manager.R.id;
import task.manager.R.layout;
import task.manager.model.Task;
import task.manager.model.TaskApplication;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

// TODO: Auto-generated Javadoc
/**
 * The Class EnhancedListAdapter.
 */
@SuppressWarnings("unused")
public class EnhancedListAdapter extends BaseAdapter {

	/** The m context. */
	private Context mContext;

	/** The dates. */
	private ArrayList<String> items, dates;
	
	/** The filtered list. */
	private LinkedList<Task> filteredList;

	/**
	 * Instantiates a new enhanced list adapter.
	 *
	 * @param mContext the m context
	 * @param listItems the list items
	 * @param listDates the list dates
	 * @param filter the filter
	 */
	public EnhancedListAdapter(Context mContext, ArrayList<String> listItems,
			ArrayList<String> listDates, LinkedList<Task> filter) {
		this.mContext = mContext;
		this.items = listItems;
		this.dates = listDates;
		this.filteredList = filter;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v;
		TextView tv;
		TextView tv2;
		CheckBox cb;
		v = setupView(convertView);

		tv2 = (TextView) v.findViewById(R.id.icon_text);
		tv = (TextView) v.findViewById(R.id.displayDate);
		cb = (CheckBox) v.findViewById(R.id.checkBox1);
		tv.setText(this.items.get(position));
		tv2.setText(this.dates.get(position));
		
		TaskApplication facade = TaskApplication.getInstance();
		LinkedList<Task> tasks = facade.getTaskList();
		if(tasks.get(filteredList.get(position).getIndex()).getCompleted())
			cb.setChecked(true);
		
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		    	TaskApplication facade = TaskApplication.getInstance();
		    	LinkedList<Task> tasks = facade.getTaskList();
		        if (isChecked){
		        	tasks.get(filteredList.get(position).getIndex()).setCompleted(true);
		    	}
		        else
		        	tasks.get(filteredList.get(position).getIndex()).setCompleted(false);
		        try {
					facade.saveBinary();
				} catch (IOException e) {
					e.printStackTrace();
				}
		    }
		});
		
		return v;
	}

	/**
	 * Setup view.
	 *
	 * @param convertView the convert view
	 * @return the view
	 */
	private View setupView(View convertView) {
		View v;
		final LayoutInflater li = (LayoutInflater) this.mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			// if it's not recycled, initialize some attributes
			v = li.inflate(R.layout.list_item_enhanced, null);
		} else {
			v = convertView;
		}
		return v;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return this.items.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public String getItem(int position) {
		return this.items.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return this.items.get(position).hashCode();
	}
}
